package com.rc.myapplication.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.rc.myapplication.R;
import com.rc.myapplication.activity.HomeActivity;
import com.rc.myapplication.adapter.ImageAdapter;
import com.rc.myapplication.view.CenterShowHorizontalScrollView;

public class HomeFragment extends Fragment implements View.OnClickListener {
    Activity activity;
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = (Activity) context;
    }

    View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_main,null);
        initView();
        initHori();
        return view;


    }


    EditText editText;
    CenterShowHorizontalScrollView mHScrollView;
    ImageView imageView;
    ImageView closeImage;
    View top;
    SwipeRefreshLayout swipeRefreshLayout;
    GridView gridView;
    GridView gridView1;
    void initView(){
        editText=(EditText)view.findViewById(R.id.search);
        Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
        drawable .setBounds(5, 2, 25, 25);//第一个 0 是距左边距离，第二个 0 是距上边距离，40 分别是长宽
        editText .setCompoundDrawables(drawable , null, null, null);//只放左边
        mHScrollView = (CenterShowHorizontalScrollView) view.findViewById(R.id.hl);
        imageView = (ImageView)view.findViewById(R.id.cate);
        top = view.findViewById(R.id.top);
        gridView1 = (GridView)view.findViewById(R.id.grid);
        gridView1.setAdapter(new ImageAdapter(activity));
        final View view = LayoutInflater.from(activity).inflate(R.layout.popup,null);
        final PopupWindow pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        pop.setAnimationStyle(R.style.popwin_anim_style);
        closeImage = (ImageView)view.findViewById(R.id.close);
        gridView = (GridView)view.findViewById(R.id.grid);
        gridView.setAdapter(new ImageAdapter(activity));
        closeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                pop.dismiss();

            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pop.isShowing()){
                    pop.dismiss();
                }else {
                    pop.showAsDropDown(top);
                }

            }
        });
        swipeRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.swipe_refresh_whats_going_on);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);
                    }
                },2000);
            }
        });
        initHori();
    }
    void initHori(){
        for (int i = 0; i < 50; i++) {
            final TextView titleItem = (TextView) View.inflate(activity, R.layout.work_time_title_item, null);
            titleItem.setTextColor(Color.parseColor("#000000"));
            mHScrollView.addItemView(titleItem, i);
            titleItem.setOnClickListener(HomeFragment.this);
            titleItem.setText(i+"");

            LinearLayout.LayoutParams titleParams = (LinearLayout.LayoutParams) titleItem.getLayoutParams();
            titleParams.leftMargin = 10;
            titleParams.rightMargin = 10;
        }

    }

    @Override
    public void onClick(View v) {

        for (int i =0;i<((LinearLayout)mHScrollView.getChildAt(0)).getChildCount();i++){
            ((TextView)((LinearLayout)mHScrollView.getChildAt(0)).getChildAt(i)).setTextColor(Color.parseColor("#000000"));
        }
        ((TextView)v).setTextColor(Color.parseColor("#FFAEB9"));
        mHScrollView.onClicked(v);
    }
}
